<?php
class RedisDb extends CApplicationComponent{
    public $host = '127.0.0.1';
    public $port = 6379;
    public $timeout = 0;
    public $pconnect = false;
    protected $_redisPhpClient;
    
    public function init(){
        parent::init();        
        $this->_redisPhpClient = new Redis();
        $this->connect($this->pconnect);
    }
    
    public function __call($name, $parameters){
        return call_user_func_array(array($this->redis(),$name), $parameters);
    }
    
    public function redis(){
        return $this->_redisPhpClient;
    }    
    public function connect($pconnect = false,$host = null,$port = null,$timeout = null){
        if (!empty($host)){
            $this->host = $host;
        }
        if (!empty($port)){
            $this->port = $port;
        }
        if (!empty($timeout)){
            $this->timeout = $timeout;
        }
        
        if ($pconnect){
            $this->redis()->connect($this->host,$this->port,$this->timeout);
        }else{
            $this->redis()->pconnect($this->host,$this->port,$this->timeout);
        }
        
        return $this->redis();
    }
    
}